layui.use(['form', 'layer', 'table'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#albumList',
        url: '/manage/album/data',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limit: 5,
        limits: [10, 15, 20, 25],
        id: "albumListTable",
        cols: [[
            {type: "checkbox", fixed: "left", width: 50},
            {field: 'id', title: '编号', align: "left"},
            {field: 'name', title: '相册名称', align: "left"},
            {
                field: 'showImg', title: '封面图', width: 180, align: "center", templet: function (d) {
                    return '<img style="width: 100px;" src="/upload/album/cover/' + d.showImg + '" height="40" />';
                }
            },

            {
                field: 'showFlag', title: '公开', align: 'center', templet: function (d) {
                    return '<input type="checkbox" name="showFlag" lay-filter="showFlag" lay-skin="switch" switch_id="' + d.id + '" lay-text="是|否" ' + d.showFlag + '>'
                }
            },
            {field: 'creator', title: '创建人', align: 'left'},
            {field: 'createTime', title: '创建时间', align: 'left'},

            {field: 'imgNum', title: '数量', align: 'left'},
            {title: '操作', templet: '#albumListBar', fixed: "right", align: "center"}
        ]]
    });

    //是否首页展示
    form.on('switch(showFlag)', function (data) {
        var index = layer.msg('修改中，请稍候', {icon: 16, time: false, shade: 0.8});
        setTimeout(function () {
            layer.close(index);
            //自定义switch_id 作为主键 用作修改内容唯一标识
            var id = data.elem.attributes['switch_id'].nodeValue;
            $.ajax({
                //几个参数需要注意一下
                type: "POST",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "/manage/album/show",//url
                data: {
                    id: id,
                },
                success: function (result) {
                    if (result.flag == "success") {
                        layer.msg("操作成功！", {icon: 1});
                    } else {
                        layer.msg("操作失败！", {icon: 2});
                    }
                    tableIns.reload();
                }, error: function () {
                    layer.msg("操作失败，请重试！", {icon: 2});
                }
            });
        }, 500);
    });


    //添加文章
    function add() {
        var index = layui.layer.open({
            title: "新建相册",
            type: 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area: ["400px", "500px"],
            content: "/manage/album/add",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }

    function update(data) {
        var index = layui.layer.open({
            title: "修改相册信息",
            type: 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area: ["400px", "500px"],
            content: "/manage/album/update?id=" + data.id,
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }

    $(".add_btn").click(function () {
        add();
    });

    $(".editAlbumImg_btn").click(function () {
        var checkStatus = table.checkStatus('albumListTable'),
            data = checkStatus.data;
        if (data.length == 1) {
            var id = data[0].id;
            var win = window.open();
            $.ajax({
                url: '/manage/albumimg/img',
                type: 'post',
                data: {
                    id: id
                },
                asyn: false
            }).done(function (data) {
                var doc = win.document;
                doc.write(data);    // !!! 关键，接收后端的数据并进行新窗口写入
                doc.close();
            });
        } else if (data.length > 1) {
            layer.msg("只能选择一条记录进行内容管理，请重新选择！", {icon: 7});
        } else {
            layer.msg("请选择一条记录！", {icon: 7});
        }
    });


    //列表操作
    table.on('tool(albumList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'edit') { //编辑
            update(data);
        } else if (layEvent === 'del') {
            //删除
            if (data.imgNum > 0) {
                layer.msg("该相册下存在照片，不允许删除！", {icon: 5});

            } else {
                layer.confirm('确定删除此相册？', {icon: 3, title: '提示信息'}, function (index) {
                    $.post("/manage/album/delete", {
                        id: data.id
                    }, function (data) {
                        data = JSON.parse(data);
                        if (data.flag === "success") {
                            layer.msg("删除成功", {icon: 1});
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.msg("删除失败！", {icon: 2});
                        }
                    })
                });
            }
        }
    });
});